﻿@using System.Text
@using BlazorComponent.I18n

<PCron Value="@_cronValue" ValueChanged="OnCronValueChanged">
</PCron>

<div>
    <MTextField Value="_cronValue" TValue="string" ValueChanged="OnCronValueChanged" Label="@i18n.T("$masaBlazor.cron.demo.cronExpressionLabel")" Outlined Dense></MTextField>
    <MTextarea @bind-Value="_nextTenRunTime" Readonly Outlined Rows="11" Label="@i18n.T("$masaBlazor.cron.demo.nextTenRunTime")">
    </MTextarea>
</div>

@code {
    [Inject]
    I18n i18n { get; set; }

    [CascadingParameter(Name = "TimezoneOffset")]
    public TimeSpan TimezoneOffset { get; set; }

    string _cronValue = string.Empty;
    string _nextTenRunTime = string.Empty;
    StringBuilder sb = new ();

    private Task OnCronValueChanged(string cronValue)
    {
        if(_cronValue != cronValue)
        {
            _cronValue = cronValue;

            GetNextTenRunTime();
        }
        return Task.CompletedTask;
    }

    private void GetNextTenRunTime()
    {
        sb.Clear();
        _nextTenRunTime = string.Empty;

        var startTime = DateTimeOffset.Now;

        var cronExpression = new CronExpression(_cronValue);

        var allTimeZone = TimeZoneInfo.GetSystemTimeZones();

        var timezoneInfo = allTimeZone.FirstOrDefault(p => p.BaseUtcOffset == TimezoneOffset);

        cronExpression.TimeZone = timezoneInfo;

        for (int i = 0; i < 10; i++)
        {
            var nextExecuteTime = cronExpression.GetNextValidTimeAfter(startTime);

            if (nextExecuteTime.HasValue)
            {
                startTime = nextExecuteTime.Value;
                sb.AppendLine(startTime.ToOffset(TimezoneOffset).ToString("yyyy-MM-dd HH:mm:ss"));
            }
        }

        if(sb.Length == 0)
        {
            _nextTenRunTime = "No more next run time";
        }
        else
        {
            _nextTenRunTime = sb.ToString();
        }
    }
}